$(document).ready(function () {
	fetchOneBookData();
	fetchOneBookComments();

    $("#comment-list-panel").click(function(e) {
	    $.ajax({
	        type: "GET",
	        data: {BID: $("#BID").val(), PageIndex: 1, PageSize: 20},
	        url: "../Handle/EvaluateHandlerService.ashx?method=Get_One_Book_Evaluate_From_BID",
	        dataType: "json",
	        success: function (data) {
	        	//console.log(data);
	        	if(0 == data.rows.length) {
	        		$("#book-comment-list").text("本书暂无评论");
	        	} else {
	        		var outputHtml = getCommentListOutputHtml(data.rows);
	        		$("#book-comment-list").html(outputHtml);
	        	}
	        }
	    });
    });
});

function fetchOneBookData() {
	var params = GetRequest();
	var BID = "";
	for(var i in params) {//不使用过滤
        BID = params[i];
        break;
    }
	if ("" == BID) {return;}
	$("#BID").val(BID);
    $.ajax({
        type: "GET",
        data: {BID: BID},
        url: "../Handle/BookHandlerService.ashx?method=Get_One_Book_Info_From_BID",
        dataType: "json",
        success: function (data) {
            //console.log(data.rows[0]);
        	data = data.rows[0];
            $("#book-id").text(data["BID"]);
        	$("#book-name").text(data["Bname"]);
        	$("#book-img").attr('src',data["Pname"]);
        	$("#book-author").text(data["Author"]);
        	$("#book-press").text(data["Press"]);
        	$("#book-pym").text(data["PYM"]);
        	$("#book-price").text(data["Price"]+"元");
        	$("#book-isbn").text(data["ISBN"]);
        	$("#book-cid").text(data["cname"]);
            $("#book-desc").text(data["Descript"]);
        	$("#book-owner").text(data["rxnf"]+data["cid"]+"班 "+data["sname"]);
        }
    });
}

function GetRequest() {  
   var url = location.search; //获取url中"?"符后的字串  
   var theRequest = new Object();  
   if (url.indexOf("?") != -1) {  
      var str = url.substr(1);  
      strs = str.split("&");  
      for(var i = 0; i < strs.length; i ++) {  
         theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);  
      }  
   }  
   return theRequest;  
}

function fetchOneBookComments() {
	$.ajax({
        type: "GET",
        data: {BID: $("#BID").val(), PageIndex: 1, PageSize: 20},
        url: "../Handle/EvaluateHandlerService.ashx?method=Get_One_Book_Evaluate_From_BID",
        dataType: "json",
        success: function (data) {
        	// console.log(data);
        	if(0 == data.rows.length) {
        		$("#book-comment-list").text("本书暂无评论");
        	} else {
        		var outputHtml = getCommentListOutputHtml(data.rows);
        		$("#book-comment-list").html(outputHtml);
        	}
        }
    });
}

function getCommentListOutputHtml(items) {
    var outputHtml = "";
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        
        outputHtml += "<div class='panel panel-warning'><div class='panel-heading'>"+
                       "<div class='row'><div class='col-md-4 col-xs-4'>"+item["GradeName"]+item["ClassName"]+"班 "+item["StudentName"]+"</div>"+
                        "<div class='col-md-4 col-xs-4 text-center'>" +item["egrade"] +"颗星</div>"+
                        "<div class='col-md-4 col-xs-4 text-right'>"+"<span>"+item["bdate"]+"</span></div></div></div>"+
                        "<div class='panel-body'>"+item["econtent"]+"</div></div>";
    }
    return outputHtml;
}

document.addEventListener("error", function (e) {
    var elem = e.target;
    if (elem.tagName.toLowerCase() == 'img') {
        elem.src = "../images/book-default-medium.gif";
    }
}, true);
